package cn.eeepay.boss.action.func;

import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.common.json.JSON;

import cn.eeepay.boss.system.SystemLog;
import cn.eeepay.framework.model.Result;
import cn.eeepay.framework.model.ShareDeductionSet;
import cn.eeepay.framework.model.ShareDeductionSetVo;
import cn.eeepay.framework.service.BossSysConfigService;
import cn.eeepay.framework.service.unTransactionalImpl.job.ShareDeductionUpdateValueJob;

@Controller
@RequestMapping("/shareDeductionSet")
public class ShareDeductionSetAction {

	@Resource
	private BossSysConfigService bossSysConfigService;

	@RequestMapping("/home")
	@ResponseBody
	public Result home() {
		Result result = new Result();
		String modifyValue = bossSysConfigService
				.selectValueByKey(ShareDeductionUpdateValueJob.FUNCTION_MANAGE_076_MODIFY);

		try {
			ShareDeductionSet modifyShareDeductionSet = JSON.parse(modifyValue, ShareDeductionSet.class);
			String modifyRate = modifyShareDeductionSet.getRate();
			
			ShareDeductionSetVo shareDeductionSetVo = new ShareDeductionSetVo();
			shareDeductionSetVo.setModifyRate(modifyRate);
			
			String oldValue = bossSysConfigService.selectValueByKey(ShareDeductionUpdateValueJob.FUNCTION_MANAGE_076);
			ShareDeductionSet oldShareDeductionSet = JSON.parse(oldValue, ShareDeductionSet.class);
			shareDeductionSetVo.setRate(oldShareDeductionSet.getRate());
			shareDeductionSetVo.setEffectiveDate(oldShareDeductionSet.getEffectiveDate());
			result.setStatus(true);
			result.setData(shareDeductionSetVo);
		} catch (Exception e) {
			result.setStatus(false);
			result.setMsg("系统异常");
			e.printStackTrace();
		}

		return result;
	}

	@RequestMapping("/save")
	@ResponseBody
	@SystemLog(description = "分润扣税比例修改",operCode="shareDeductionSet.save")
	public Result save(String rate) {
		Result result = new Result();
		ShareDeductionSet shareDeductionSet = new ShareDeductionSet();
		shareDeductionSet.setRate(rate);
		try {
			String json = JSON.json(shareDeductionSet);
			bossSysConfigService.updateValueByKey(ShareDeductionUpdateValueJob.FUNCTION_MANAGE_076_MODIFY, json);
			result.setStatus(true);
			result.setMsg("保存成功");
		} catch (IOException e) {
			result.setStatus(false);
			result.setMsg("系统异常");
			e.printStackTrace();
		}
		return result;
	}

}
